/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.tree;

import org.rails.extjs.Observable;
import org.rails.extjs.Parameters;
import org.rails.extjs.javascript.Function;

public class TreeLoader extends Observable {

	public TreeLoader() {
		super();
	}
	
	public TreeLoader(String requestMethod,String url,Parameters baseParams) {
		super();
		setRequestMethod(requestMethod);
		setUrl(url);
		setBaseParams(baseParams);
	}
	
	public void setBaseAttrs(Object baseAttrs){
		addOption("baseAttrs", baseAttrs);
	}
	
	public Object getBaseAttrs(){
		return getOption("baseAttrs");
	}
	
	public void setBaseParams(Parameters baseParams){
		addOption("baseParams",baseParams);
	}
	
	public Parameters getBaseParams(){
		return (Parameters)getOption("baseParams");
	}
	
	public void setClearOnLoad(Boolean clearOnLoad){
		addOption("clearOnLoad", clearOnLoad);
	}
	
	public Boolean isClearOnLoad(){
		return getBoolean("clearOnLoad",true);
	}
	
	public void setUrl(String url){
		addOption("url",url);
	}
	
	public String getUrl(){
		return getString("url","");
	}
	
	public void setDirectFn(Function directFn){
		addOption("directFn", directFn);
	}
	
	public Function getDirectFn(){
		return (Function)getOption("directFn");
	}

	public void setParamsAsHash(Boolean paramsAsHash){
		addOption("paramsAsHash", paramsAsHash);
	}
	
	public Boolean isParamsAsHash(){
		return getBoolean("paramsAsHash",false);
	}
	
	public void setPreloadChildren(Boolean preloadChildren){
		addOption("preloadChildren", preloadChildren);
	}
	
	public Boolean isPreloadChildren(){
		return getBoolean("preloadChildren",true);
	}
	
	public void setRequestMethod(String requestMethod){
		addOption("requestMethod", requestMethod);
	}
	
	public String getRequestMethod(){
		return getString("requestMethod","GET");
	}
	
	
	@Override
	public String getScript() {
		createItems();
		script = new StringBuffer("");
		script.append("new Ext.tree.TreeLoader(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
